#ifndef CANASCLOGGER_H
#define CANASCLOGGER_H

#include <QObject>
#include <QFile>
#include <QTextStream>
#include <QMutex>
#include <QDateTime>
#include <QHostAddress>

class CanAscLogger : public QObject
{
    Q_OBJECT
public:
    explicit CanAscLogger(QObject *parent = nullptr);
    ~CanAscLogger() override;

    // 初始化日志文件，返回是否成功
    bool initLogFile(const QString &baseFileName = "CAN_Log", const QString &mode = "ASC");

    // 关闭日志文件
    void closeLogFile();

    // 记录接收的CAN数据
    void logReceivedData(quint32 msgId, const QByteArray &data,
                        const QString &msgName = "", const QString &sigName = "",
                        double physValue = 0, const QString &valueDesc = "");

    // 记录发送的CAN数据
    void logSentData(quint32 msgId, const QByteArray &data,
                    const QString &msgName = "");

    //记录相机datagram数据的方法
    void logCameraDatagram(const QByteArray &datagram, const QHostAddress &sender,
                    quint16 senderPort, bool isFromServer);

private:
    QFile *m_ascFile;               // ASC文件对象
    QTextStream *m_ascStream;       // 文本流
    QMutex m_fileMutex;             // 线程安全锁
    int m_channel;                  // CAN通道号，默认为1
    QString m_mode; // 保存当前模式

    // 生成带时间戳的文件名
    QString generateFileName(const QString &baseName, const QString &mode = "ASC");

    // 格式化当前时间
    QString getFormattedTime();

    QString formatDataForCsv(const QByteArray &data);
};

#endif // CANASCLOGGER_H
